﻿module HttpTests

open System.IO
open NUnit.Framework;
open Http;
open HttpInterfaces

[<TestFixture>]
type testParseResponse() =

    [<Test>]
    member this.testParseResponse() =
        let html = """HTTP/1.1 200 OK
Content-Type: text/html;charset=iso-8859-1

<!doctype html><title>Hello</title>"""
        let bytes = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html));
        // now parse
        let response = parseResponse(bytes)
        Assert.AreEqual(200, response.statusCode)
        let (TextBody(text)) = response.body
        Assert.AreEqual("<!doctype html><title>Hello</title>", text)

    [<Test>]
    member this.testParseResponse2() =
        let html = """HTTP/1.1 302 OK
Location: http://example.com/

"""
        let bytes = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html));
        // now parse
        let response = parseResponse(bytes)
        Assert.AreEqual(302, response.statusCode)
        Assert.AreEqual("http://example.com/", response.headers.["Location"])
        ()

    [<Test>]
    member this.testParseChuncked() =
        let html = """HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/html

27
<!doctype html><html><head><title>Hello
2A
</title></head><body>Goodbye</body></html>
0

"""
        let bytes = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html));
        // now parse
        let response = parseResponse(bytes)
        Assert.AreEqual(200, response.statusCode)
        let (TextBody(text)) = response.body
        Assert.AreEqual("""<!doctype html><html><head><title>Hello</title></head><body>Goodbye</body></html>""", text)
        ()

